//
//  YBPortManager.m
//  Moonlight
//
//  Created by 清风徐来 on 2023/6/25.
//  Copyright © 2023 Moonlight Game Streaming Project. All rights reserved.
//

#import "YBPortManager.h"
#include "PortManager.h"

@implementation YBPortManager

/// 开始处理端口转换
/// - Parameters:
///   - ntpcport: 内网TCP
///   - wtpcport: 外网TCP
///   - nudpport: 内网UDP
///   - wudpport: 外网UDP
+(void)dealWithNtpcport:(NSString *)ntpcport wtpcport:(NSString *)wtpcport nudpport:(NSString *)nudpport wudpport:(NSString *)wudpport {
    if (!ntpcport || !wtpcport || !nudpport || !wudpport) {
        NSLog(@"数据异常");
        return;
    }
    
    if (![ntpcport containsString:@"#"] || ![wtpcport containsString:@"#"] || ![nudpport containsString:@"#"] || ![wudpport containsString:@"#"]) {
        NSLog(@"数据异常");
        return;
    }
    
    NSArray *ntcpArray = [ntpcport componentsSeparatedByString:@"#"];
    NSArray *wtpcArray = [wtpcport componentsSeparatedByString:@"#"];
    for (int i = 0; i < ntcpArray.count; i ++) {
        NSString *port = ntcpArray[i];
        [self getCuctomPort:port customPort:wtpcArray[i]];
    }
    
    NSArray *nudpArray = [nudpport componentsSeparatedByString:@"#"];
    NSArray *wudpArray = [wudpport componentsSeparatedByString:@"#"];
    for (int i = 0; i < nudpArray.count; i ++) {
        NSString *port = nudpArray[i];
        [self getCuctomPort:port customPort:wudpArray[i]];
    }
    
    // 打印信息
    printPort();
}


+(void)getCuctomPort:(NSString *)originPort customPort:(NSString *)customPort {
    switch (originPort.intValue) {
            // TCP ports
        case 47984: {
            customPort47984 = customPort.intValue;
        }
            break;
            
        case 47989: {
            customPort47989 = customPort.intValue;
        }
            break;
            
        case 48010: {
            customPort48010 = customPort.intValue;
        }
            break;
            
            // UDP ports
        case 47998: {
            customPort47998 = customPort.intValue;
        }
            break;
            
        case 47999: {
            customPort47999 = customPort.intValue;
        }
            break;
            
        case 48000: {
            customPort48000 = customPort.intValue;
        }
            break;
            
        case 48002: {
            customPort48002 = customPort.intValue;
        }
            break;

        default:
            break;
    }
}

@end
